package face.array;

/**
 * @author spf
 * @date 2024/8/20
 * @time 21:47
 */

/**
 * 最小子数组长度
 */
public class minSubAaaryLen {

    public static void main(String[] args) {
        int[] a = {2, 3, 1, 2, 4, 3};
        int target = 7;
        System.out.println(minSubArrayLen(target, a));
    }

    private static int minSubArrayLen(int target, int[] nums) {
        int left = 0;
        int right = 0;
        int sum = 0;
        int result = Integer.MAX_VALUE;

        while (right < nums.length) {
            sum += nums[right];

            while (sum >= target) {
                result = Math.min(result, right - left + 1);
                sum -= nums[left];
                left++;
            }

            right++;
        }

        return result == Integer.MAX_VALUE ? 0 : result;
    }

}
